///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2011 Gabriel Montagné Láscaris-Comneno and Alberto
//  Brealey-Guzmán.
//
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package com.rojored.preloaders
{

/**
 *  Simple preloader that uses 2 boxes of different colors to indicate
 *  progress. Multiple colors and alphas are configurable for the left and
 *  right sides to achieve a flickery effect.
 */
public class SweepProgressDisplay extends StagedPreloaderBase 
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function SweepProgressDisplay() 
    {
        super();

        stepProgressTo = 1;
        easingFactor = 0.2;
    }

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //--------------------------------------
    //  colorsLeft
    //--------------------------------------

    /**
     *  Colors used for the left side of the progress box. A random color will
     *  be picked from one of these every time the progress display is updated.
     */
    public var colorsLeft:Array = [0x103040];

    //--------------------------------------
    //  colorsRight
    //--------------------------------------

    /**
     *  Colors used for the right side of the progress box. A random color
     *  will be picked from one of these every time the progress display is
     *  updated.
     */
    public var colorsRight:Array = [0x999999];
    
    //--------------------------------------
    //  alphasLeft
    //--------------------------------------

    /**
     *  Alpha values used for the left side of the progress box. A random
     *  value will be picked from one of these and applied to the color box
     *  every time the progress display is updated.
     */
    public var alphasLeft:Array = [0.65, 0.5, 0.7];

    //--------------------------------------
    //  alphasRight
    //--------------------------------------

    /**
     *  Alpha values used for the right side of the progress box. A random
     *  value will be picked from one of these and applied to the color box
     *  every time the progress display is updated.
     */
    public var alphasRight:Array = [0.2, 0.1];

    //--------------------------------------
    //  rightToLeft
    //--------------------------------------

    /**
     *  If <code>true</code> the progress will be displayed from right to
     *  left.
     */
    public var rightToLeft:Boolean = false;

    //--------------------------------------------------------------------------
    //
    //  Overriden methods
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    override protected function drawProgress(progress:Number):void 
    {
        var progressX:Number = stageWidth * progress;

        if (rightToLeft)
            progressX = stageWidth - progressX;

        graphics.clear();
        graphics.beginFill(
            colorsLeft[int(Math.random() * colorsLeft.length)], 
            alphasLeft[int(Math.random() * alphasLeft.length)]
        );
        graphics.drawRect(0, 0, progressX, stageHeight);
        graphics.endFill();

        graphics.beginFill(
            colorsRight[int(Math.random() * colorsRight.length)], 
            alphasRight[int(Math.random() * alphasRight.length)]
        );
        graphics.drawRect(progressX, 0, stageWidth - progressX, stageHeight);
        graphics.endFill();
    }
}
}
